import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class RedisClient {
    static Socket s;
    static PrintWriter writer;
    static BufferedReader reader;


    public static void main(String[] args) {
        try{
            //1.建立连接
            String host = "127.0.0.1";
            int port = 6379;
            s = new Socket(host,port);

            //2.获取输出流、输入流
            writer = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8));
            reader = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));

            //3.发出请求
            //3.1.获取授权 auth 123456
            sendRequest("auth","123456");
            Object obj = handleResponse();
            System.out.println("obj = "+ obj);

            //3.2：set name jack
            sendRequest("set","name","jack");

            //4.解析响应
            obj = handleResponse();
            System.out.println("obj = " + obj);

            //5：get name
            sendRequest("get","name");

            //6.解析响应
            obj = handleResponse();
            System.out.println("obj = "+obj);

            //5：get name
            sendRequest("mget","name","num","msg");

            //6.解析响应
            obj = handleResponse();
            System.out.println("obj = "+obj);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放连接
            try{
                if(reader != null) reader.close();
                if(writer != null) writer.close();
                if(s != null) s.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    private static Object handleResponse() throws IOException {
        //读取首字节
        int prefix = reader.read();
        //判断数据类型标示
        switch (prefix){
            case '+': //单行字符串，直接读一行。
                return reader.readLine();
            case '-': //异常，读取一行。
                return reader.readLine();
            case ':': //数字。
                return Long.parseLong(reader.readLine());
            case '$': //多行字符串。
                //先读取长度。
                int len = Integer.parseInt(reader.readLine());
                if( len == -1){
                    return null;
                }
                if( len == 0){
                    return "";
                }
                //再读数据，读len个字节，我们加黑色没有特殊字符，这里简化读取一行。
                return reader.readLine();
            case '*': // 读取多行。
                return readBulkString();
            default:
                throw new RuntimeException("错误的数据格式！");
        }
    }

    private static Object readBulkString() throws IOException {
        //获取数组大小。
        int len = Integer.parseInt(reader.readLine());
        if(len <= 0){
            return null;
        }
        //定义集合，接收多个元素。
        ArrayList<Object> list = new ArrayList<>(len);
        //遍历，一次读取每个元素。
        for (int i = 0; i < len; i++) {
            list.add(handleResponse());
        }
        return list;
    }

    private static void sendRequest(String ... args) {
        writer.println("*" +args.length);
        for (String arg : args) {
            writer.println("$" + arg.getBytes(StandardCharsets.UTF_8).length);
            writer.println(arg);
        }
        writer.flush();
    }

}
